<template>
  <div>
    <el-dialog
      :title="ruleForm.id == '' ? '添加设备备件' : '编辑设备备件'"
      :visible.sync="addEditIsDialog_c"
      :close-on-click-modal="false"
      @close="resetForm('formName')"
      width="800px"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="formName"
        label-width="120px"
        class="demo-ruleForm"
        :class="{ 'form-rules-show': seeType }"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="备件名称：" prop="name">
              <el-input
                v-model="ruleForm.name"
                placeholder="请输入备件名称"
                clearable
                maxlength="50"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备件参数：" prop="param">
              <el-input
                v-model="ruleForm.param"
                placeholder="请输入备件参数"
                clearable
                maxlength="50"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备件数量：" prop="num">
              <el-input-number
                v-model="ruleForm.num"
                placeholder="请输入备件数量"
                :precision="0"
                :step="1"
                :max="9999999"
                :min="0"
                :disabled="seeType"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="预警数量：" prop="warnNum">
              <el-input-number
                v-model="ruleForm.warnNum"
                placeholder="请输入预警数量"
                :precision="0"
                :step="1"
                :max="9999999"
                :min="0"
                :disabled="seeType"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="生产日期：" prop="productionDate">
              <el-date-picker
                v-model="ruleForm.productionDate"
                type="date"
                placeholder="请选择生产日期"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                clearable
                :disabled="seeType"
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="使用周期：" prop="serviceCycle">
              <el-input-number
                v-model="ruleForm.serviceCycle"
                placeholder="请输入使用数量"
                :precision="0"
                :step="1"
                :max="9999999"
                :min="0"
                :disabled="seeType"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getDeviceSparePart, saveDeviceSparePart } from "@/api/device";

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: "",
        deviceId: "",
        name: "",
        param: "",
        num: "",
        warnNum: "",
        productionDate: "",
        serviceCycle: "",
      },
      rules: {
        // 表单规则
        name: [{ required: true, message: "请输入备件名称", trigger: "blur" }],
        param: [{ required: true, message: "请输入备件参数", trigger: "blur" }],
        num: [{ required: true, message: "请输入数量", trigger: "blur" }],
        warnNum: [
          { required: true, message: "请输入预警数量", trigger: "blur" },
        ],
        productionDate: [
          { required: true, message: "请选择生产日期", trigger: "blur" },
        ],
        serviceCycle: [
          { required: true, message: "请输入使用周期", trigger: "blur" },
        ],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm.id = "";
          this.seeType = false;
        }

        // 编辑
        if (this.addEditStatus == "edit") {
          this.ruleForm.id = this.addEditId;
          this.seeType = false;
          this.getDetail();
        }

        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.id = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }
      }
    },
  },
  methods: {
    // 详情
    getDetail() {
      getDeviceSparePart({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.data;
        }
      });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.deviceId = this.$route.query.deviceId;
          saveDeviceSparePart(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }

}
</style>
